package com.tibco.notepad.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tibco.notepad.R;
import com.tibco.notepad.domain.Notepad;

public class NotepadAdapter extends BaseAdapter {
	private LayoutInflater mLayoutInflater;
	private List<Notepad> mList;

	public NotepadAdapter(Context context, List<Notepad> data) {
		mLayoutInflater = LayoutInflater.from(context);
		if ((null == data) || (data.size() == 0)) {
			mList = new ArrayList<Notepad>();
		} else {
			mList = data;
		}

	}

	public List<Notepad> getData() {
		return this.mList;
	}

	public void setData(List<Notepad> notes) {
		this.mList = notes;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	// 追加、更新数据源
	public void addNewNote(Notepad note) {
		this.mList.add(note);
		notifyDataSetChanged();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.item, null);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.title.setText(mList.get(position).getTitle());
		holder.time.setText(mList.get(position).getTime());
		holder.content.setText(mList.get(position).getContent());

		return convertView;
	}

	private static class ViewHolder {
		private TextView title, time, content;
	}

}
